package com.uniriho.lxr0811_7300_bl.ui.systeminfo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.uniriho.lxr0811_7300_bl.base.Constant
import com.uniriho.lxr0811_7300_bl.databinding.FragmentSysteminfoBinding

class Fragment_SystemInfo : Fragment(), View.OnClickListener {

    private lateinit var textView: TextView
    private lateinit var btn_sysCPUID: Button
    private lateinit var btn_sysVS: Button
    private lateinit var btn_sysTime: Button
    private lateinit var text_sysLED1: Button
    private lateinit var text_sysLED2: Button
    private lateinit var text_sysLED3: Button
    private lateinit var text_sysLED4: Button
    private var led1State:Boolean  = false
    private var led2State:Boolean  = false
    private var led3State:Boolean  = false
    private var led4State:Boolean  = false
    private var systemInfoBinding: FragmentSysteminfoBinding? = null

    private val viewModelSystemInfo by lazy {
        ViewModelProvider(this).get(ViewModel_SystemInfo::class.java)
    }


    // This property is only valid between onCreateView and
    // onDestroyView.


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        systemInfoBinding = FragmentSysteminfoBinding.inflate(inflater, container, false)
        val root: View = systemInfoBinding!!.root
        initView()
        initData()
        initEvent()
        return root
    }

    private fun initEvent() {
        btn_sysCPUID.setOnClickListener(this)
        btn_sysVS.setOnClickListener(this)
        btn_sysTime.setOnClickListener(this)
        text_sysLED1.setOnClickListener(this)
        text_sysLED2.setOnClickListener(this)
        text_sysLED3.setOnClickListener(this)
        text_sysLED4.setOnClickListener(this)

    }

    private fun initData() {


    }

    private fun initView() {
        textView = systemInfoBinding!!.textResult
        btn_sysCPUID = systemInfoBinding!!.btnSysCPUID
        btn_sysVS = systemInfoBinding!!.btnSysVS
        btn_sysTime = systemInfoBinding!!.btnSysTime
        text_sysLED1 = systemInfoBinding!!.btnSysLED1
        text_sysLED2 = systemInfoBinding!!.btnSysLED2
        text_sysLED3 = systemInfoBinding!!.btnSysLED3
        text_sysLED4 = systemInfoBinding!!.btnSysLED4

        viewModelSystemInfo.result.observe(viewLifecycleOwner) {
            textView.text = it
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        systemInfoBinding = null
    }

    override fun onClick(p0: View?) {
        when (p0) {
            btn_sysCPUID -> {viewModelSystemInfo.sendData(Constant.SYSTEM_CPUID)}
            btn_sysVS -> {viewModelSystemInfo.sendData(Constant.SYSTEM_VS)}
            btn_sysTime -> {viewModelSystemInfo.sendData(Constant.SYSTEM_TIME)}
            text_sysLED1 -> {
                if (led1State) {
                    viewModelSystemInfo.sendData(Constant.LED1_CLOSE)
                    led1State = false
                }else{
                    viewModelSystemInfo.sendData(Constant.LED1_OPEN)
                    led1State = true
                }
            }
            text_sysLED2 -> {
                if (led2State) {
                    viewModelSystemInfo.sendData(Constant.LED2_CLOSE)
                    led2State = false
                }else{
                    viewModelSystemInfo.sendData(Constant.LED2_OPEN)
                    led2State = true
                }
            }
            text_sysLED3 -> {
                if (led3State) {
                    viewModelSystemInfo.sendData(Constant.LED3_CLOSE)
                    led3State = false
                }else{
                    viewModelSystemInfo.sendData(Constant.LED3_OPEN)
                    led3State = true
                }
            }
            text_sysLED4 -> {
                if (led4State) {
                    viewModelSystemInfo.sendData(Constant.LED4_CLOSE)
                    led4State = false
                }else{
                    viewModelSystemInfo.sendData(Constant.LED4_OPEN)
                    led4State = true
                }
            }
        }
    }
}